package com.cx.io;

import com.sun.org.apache.xerces.internal.dom.PSVIAttrNSImpl;

import java.io.*;

/**
 * @author zhangshuai
 * @version V1.0
 * @Project Java2_26
 * @Package com.cx.io
 * @Description:
 * @Date 2025/5/23 17:14
 */

public class SeririableTest {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //序列化：将对象写入到文件中  序列化输出流对象
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("ddd.txt"));

        Person aaa = new Person("afasda但是发大水发射点", 20);
        objectOutputStream.writeObject(aaa);
        objectOutputStream.close();//关闭流
        System.out.println("序列化成功");

        //反序列化：将文件中的数据还原成对象  反序列化输入流对象
        ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("ddd.txt"));
        Person person = (Person) objectInputStream.readObject();
        System.out.println(person);
    }
}
class Person implements Serializable {
    private String name;
    private int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Person() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}